package com.REPDPMSystem.Service.Handler;

import com.REPDPMSystem.Service.ProjectService;
import com.REPDPMSystem.Entity.*;
import com.REPDPMSystem.Mapper.ProjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectHandler extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Override
    public List<Project> getProjectInfoBoss(){
        //查询所有
        return baseMapper.selectList(null);
    }
    @Override
    public List<Project> getProjectInfoSupervisor(){

        return baseMapper.selectList(null);
    }
    @Override
    public List<Project> getProjectInfoEmployee(Employee employee){
        switch(employee.getEmployee_position()){
            case 4:{
                QueryWrapper<Project> wrapper = new QueryWrapper<>();
                wrapper.eq("model_employee_id",employee.getStaff_id());
                return baseMapper.selectList(wrapper);
            }
            case 5:{
                QueryWrapper<Project> wrapper = new QueryWrapper<>();
                wrapper.eq("rendering_employee_id",employee.getStaff_id());
                return baseMapper.selectList(wrapper);
            }
            case 6:{
                QueryWrapper<Project> wrapper = new QueryWrapper<>();
                wrapper.eq("post_production_employee_id",employee.getStaff_id());
                return baseMapper.selectList(wrapper);
            }
        }
        return null;
    }
    @Override
    public List<Project> getProjectInfoApprentice(){
        List<Project>plist = new ArrayList<>();
        for(Project p:baseMapper.selectList(null)){
            p.setPrice(-1.0);
            plist.add(p);
        }
        return plist;
    }

    @Override
    public boolean changeProject(Project project) {
        project.setProject_id(UUID.randomUUID().toString());
        return baseMapper.updateById(project)>0;
    }
    @Override
    public boolean createProject(Project project){

        project.setProject_id(UUID.randomUUID().toString());
        String nullstr = "000000000000000000000000000000000000";
        project.setModel_employee_id(nullstr);
        project.setPost_production_employee_id(nullstr);
        project.setRendering_employee_id(nullstr);
        project.setDate(System.currentTimeMillis());
        return baseMapper.insert(project)>0;
    }

    @Override
    public boolean projectAllocation(Project project, Employee employee) {
        if(employee.getEmployee_position()>4||employee.getEmployee_position()>6)return false;
        String nullstr = "000000000000000000000000000000000000";
        switch(employee.getEmployee_position()){
            case 4:{
                if(project.getModel_employee_id()==nullstr) {
                    project.setModel_employee_id(employee.getStaff_id());
                    return baseMapper.updateById(project)>0;
                }
                break;
            }
            case 5:{
                if(project.getRendering_employee_id()==nullstr) {
                    project.setRendering_employee_id(employee.getStaff_id());
                    return baseMapper.updateById(project)>0;
                }
                break;
            }
            case 6:{
                if(project.getPost_production_employee_id()==nullstr) {
                    project.setPost_production_employee_id(employee.getStaff_id());
                    return baseMapper.updateById(project)>0;
                }
                break;
            }
        }
        return false;
    }

    @Override                                                                 //project_status>employeeDepartment
    public long countDoneProjectByEmployeeAndTimePeriod(int ePosition,String employeeId,int employeeDepartment ,long startTime, long endTime) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date", startTime, endTime) // 查询条件：时间段
                    .gt("project_status",employeeDepartment);
        switch(ePosition){
            case 4:{
                queryWrapper.eq("model_employee_id",employeeId);
                break;
            }
            case 5:{
                queryWrapper.eq("rendering_employee_id",employeeId);
                break;
            }
            case 6:{
                queryWrapper.eq("post_production_employee_id",employeeId);
                break;
            }
        }
        return baseMapper.selectCount(queryWrapper); // 统计符合条件的记录数量
    }

    @Override
    public List<Project> getDoneProjectByEmployeeAndTimePeriod(int ePosition,String employeeId,int employeeDepartment , long startTime, long endTime) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date", startTime, endTime) // 查询条件：时间段
                    .gt("project_status",employeeDepartment);
        switch(ePosition){
            case 4:{
                queryWrapper.eq("model_employee_id",employeeId);
                break;
            }
            case 5:{

                queryWrapper.eq("rendering_employee_id",employeeId);
                break;
            }
            case 6:{
                queryWrapper.eq("post_production_employee_id",employeeId);
                break;
            }
        }
        return baseMapper.selectList(queryWrapper); // 统计符合条件的记录数量
    }

    @Override
    public long getDoneProjectStep(int employeeDepartment) {//大于
        return baseMapper.selectCount(
                new QueryWrapper<Project>().gt("project_status",employeeDepartment)
        );
    }

    @Override
    public long getDoingProjectStep(int employeeDepartment) {//小于等于
        return baseMapper.selectCount(
                new QueryWrapper<Project>().le("project_status",employeeDepartment)
        );
    }
}
